using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace MacViet.DataBaseAPI
{
    /// <summary>
    /// Description: Concrete DataProvider for table SPA. Provides atomic API functions.
    /// Author:  phong.huynh
    /// Created: 13/08/2010 10:33:41 SA
    /// </summary>
    public class SPADataAPI
    {
        private SQLHelper _dbTool = new SQLHelper();


        // ************************************
        // Implements function getByColumnName
        // ************************************

        public DataTable getBySPA_ID(System.Int32 SPA_ID)
        {
            SqlParameter[] parm = {new SqlParameter(SPADTO._SPA_ID, SPA_ID) };
            return _dbTool.executeReader("SP_SEL_SPA_getBySPA_ID", parm);
        }

        public DataTable getBySPA_ID_Report(System.Int32 SPA_ID, System.Int32 ACC_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._ACC_ID, ACC_ID),
                                      new SqlParameter(SPADTO._SPA_ID, SPA_ID) };
            return _dbTool.executeReader("SP_GET_BY_SPA_ID_REPORT", parm);
        }

        public DataTable getByACC_ID(System.Int32 ACC_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._ACC_ID, ACC_ID) };
            return _dbTool.executeReader("SP_SEL_SPA_getByACC_ID", parm);
        }

        public DataTable getSpaByAcc_ID(System.Int32 ACC_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._ACC_ID, ACC_ID) };
            return _dbTool.executeReader("SP_GET_SPA_NAME", parm);
        }

        public DataTable getTimeSpaByAcc_ID(System.Int32 ACC_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._ACC_ID, ACC_ID) };
            return _dbTool.executeReader("SupplierStats_TestTime", parm);
        }

        public DataTable getClientSpaByAcc_ID(System.Int32 ACC_ID, System.Int32 IdInsert)
        {
            SqlParameter[] parm = { new SqlParameter("@IdInsert", IdInsert),
                                      new SqlParameter(SPADTO._ACC_ID, ACC_ID) };
            return _dbTool.executeReader("SupplierStats_TestClient", parm);
        }
        

        public DataTable getBySPA_NAME(System.String SPA_NAME)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._SPA_NAME, SPA_NAME) };
            return _dbTool.executeReader("SP_SEL_SPA_getBySPA_NAME", parm);
        }



        // *************************************************
        // Implement function getAll/getByDynamicCondition
        // *************************************************

        public DataTable getAll()
        {
            return _dbTool.executeReader("SP_SEL_SPA_getAll");
        }

        public DataTable getAllSpa(System.Int32 ACC_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._ACC_ID, ACC_ID) };
            return _dbTool.executeReader("SP_SEL_getAll_Spa", parm);
        }


        public DataTable getSpaTempAll()
        {
            return _dbTool.executeReader("GetSpaAll");
        }

        // ********************************************************
        // Implement function getByPrimaryKeys / getByForeignKeys
        // In case table have composite primary/foreign keys
        // ********************************************************

        public DataTable getByPrimaryKeys(System.Int32 SPA_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._SPA_ID, SPA_ID) };
            return _dbTool.executeReader("SP_SEL_SPA_getByPrimaryKeys", parm);
        }

        public bool insertSPA(System.Int32 ACC_ID, System.String SPA_NAME)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._SPA_ID,1),
                                    new SqlParameter(SPADTO._ACC_ID,ACC_ID),
                                    new SqlParameter(SPADTO._SPA_NAME,SPA_NAME)};
            int effectedRows = _dbTool.ExecuteNoneQuery("SP_INS_SPA_values", parm);




            return (effectedRows == 1);
        }


        ///<summary>		
        /// Thu+.c hie^.n ca^u le^.nh update du+.a tre^n kho'a chi'nh
        ///</summary>

        public bool updateSPA(System.Int32 SPA_ID, System.Int32 ACC_ID, System.String SPA_NAME)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._SPA_ID,SPA_ID),
                                    new SqlParameter(SPADTO._ACC_ID,ACC_ID),
                                    new SqlParameter(SPADTO._SPA_NAME,SPA_NAME)};
            int affectRows = _dbTool.ExecuteNoneQuery("SP_UDP_SPA_update", parm);

            return affectRows == 1;
        }


        // ********************************************************************************
        // Implementation of deleteAll/deteleByForeignKeys/deleteByPrimaryKeys function		
        // ********************************************************************************

        // return number of deleted rows		
        public int deleteAll()
        {
            return _dbTool.ExecuteNoneQuery("SP_DEL_SPA_deleteAll");
        }

        // return number of deleted rows
        public int deleteByPrimaryKeys(System.Int32 SPA_ID)
        {
            SqlParameter[] parm = { new SqlParameter(SPADTO._SPA_ID, SPA_ID) };
            return _dbTool.ExecuteNoneQuery("SP_DEL_SPA_deleteByPrimaryKeys", parm);
        }

    }
}